//
//  ActivityListViewController.m
//  newDouban
//
//  Created by dllo on 15/10/8.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ActivityListViewController.h"
#import "ActivityDetailsViewController.h"
#import "AppTools.h"
#import "Activity.h"
#import "MBProgressHUD.h"//菊花
#import "Reachability.h"
#import "DataBaseSingTon.h"

@interface ActivityListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *activityListtableV;
@property (nonatomic, retain) NSMutableArray *activityArray;
@property (nonatomic, copy) NSString *activityPath;

@property (nonatomic, retain) MBProgressHUD *hub;


@end

@implementation ActivityListViewController


- (void)dealloc
{
    [_hub release];
    [_activityListtableV release];
    [_activityArray release];
    [super dealloc];
}


//注意, 在viewDidLoad值执行一次
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.tintColor = [UIColor purpleColor];
    self.navigationItem.title = @"活动";
    
    //NSLog(@"%f", self.view.frame.size.width);
    
    self.activityListtableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.activityListtableV.dataSource = self;
    self.activityListtableV.delegate = self;
    self.activityListtableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.activityListtableV registerClass:[ActivityListTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.activityListtableV];
    [_activityListtableV release];
    
    
    self.activityArray = [NSMutableArray array];
//    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//   self.activityPath = [document stringByAppendingPathComponent:@"activity.txt"];
//    NSLog(@"self.activityPath == %@", self.activityPath);
//    //[NSKeyedArchiver  archiveRootObject:self.activityArray toFile:self.activityPath];
//    
//    //反归档,取出model对象
//    self.activityArray = [NSKeyedUnarchiver unarchiveObjectWithFile:self.activityPath];

    [[DataBaseSingTon shareDataBase] openDBAC];
    
    
    NSString *sql = @"SELECT * FROM activity";
    NSMutableArray *arrM = [[DataBaseSingTon shareDataBase] selectActivity:sql];
    
    self.activityArray = [NSMutableArray arrayWithArray:arrM];
    [self.activityListtableV reloadData];
    
    
    
    //下拉tableView调用此方法
    [self.activityListtableV addHeaderWithCallback:^{
        
        NSLog(@"下拉");
        [self freash];
        
    }];
    [self.activityListtableV headerBeginRefreshing];

    //上拉     
    [self.activityListtableV addFooterWithCallback:^{
        
        NSLog(@"上拉");
        [self freash];
        
    }];

    
    
}


- (void)UIAlerController
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"网路连接出错" preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

- (void)freash
{
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
        case NotReachable:
            NSLog(@"没有网络连接");
            // 没有网络连接
            [self UIAlerController];
            [self.activityListtableV headerEndRefreshing];
            
            break;
        case ReachableViaWWAN:
            // 使用3G网络
            NSLog(@"使用3G网络");
            
            break;
        case ReachableViaWiFi:
            // 使用WiFi网络
            NSLog(@"使用WiFi网络");
            
            self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [self.hub show:YES];
            self.hub.labelText = @"正在加载中....";
            
            
            break;
    }

    [AppTools getDataWithURL:urlStr block:^(id result) {
        //NSLog(@"RESULT = %@", result);
        //self.activityArray = [NSMutableArray new];
        [self.activityArray removeAllObjects];
        
        
        [[DataBaseSingTon shareDataBase] dropTableAC];
        [[DataBaseSingTon shareDataBase] createActivityTable];
        
        
        NSMutableDictionary *bigDic = result;
        NSMutableArray *bigArr = [bigDic objectForKey:@"events"];
        // NSMutableArray *secondArr = [NSMutableArray array];
        for (NSDictionary *secondDic in bigArr) {
            Activity *activity = [[Activity alloc] init];
            [activity setValuesForKeysWithDictionary:secondDic];
            NSDictionary *smallDic = [secondDic objectForKey:@"owner"];
            activity.name = [smallDic objectForKey:@"name"];
            [self.activityArray addObject:activity];
            [[DataBaseSingTon shareDataBase] insertActivity:activity];
            [activity release];
        }
//        NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//        self.activityPath = [document stringByAppendingPathComponent:@"activity.txt"];
//        [NSKeyedArchiver  archiveRootObject:self.activityArray toFile:self.activityPath];
        [self.activityListtableV headerEndRefreshing];
        [self.activityListtableV footerEndRefreshing];
        [self.activityListtableV reloadData];
        //让菊花停止旋转
        [self.hub hide:YES];
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityDetailsViewController *activityDetailVC = [[ActivityDetailsViewController alloc] init];
    
    activityDetailVC.activity = [self.activityArray  objectAtIndex:indexPath.row];
    [self.navigationController pushViewController: activityDetailVC animated:YES];
    [activityDetailVC release];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 220;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.activityArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    ActivityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    Activity *activity = [self.activityArray objectAtIndex:indexPath.row];
    cell.activity = activity;
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
